package com.sinosoft.ptas.control;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sinosoft.framework.core.common.utils.StringUtil;
import com.sinosoft.framework.core.common.utils.ajax.ResponseData;
import com.sinosoft.ptas.control.dto.ProjectCotegoryData;
import com.sinosoft.ptas.entity.ProjectCotegoryEntity;
import com.sinosoft.ptas.service.ProjectCotegoryService;
import com.sinosoft.ptas.system.commom.CommonConstant;
import com.sinosoft.ptas.system.commom.ExceptionConstant;

/**
 * 项目种类控制层
 * @author zhangxiaolong
 *
 */

@Controller
@RequestMapping(value="projectCotegory")
public class ProjectCotegoryController {
	
	@Autowired 
	ProjectCotegoryService projectCotegoryService;
	
	/**
	 * 添加项目类型
	 * @param projectCotegoryData
	 * @return 项目类型传输类实体
	 */
	@ResponseBody
	@RequestMapping(value="/createProjectCotegory.service",method=RequestMethod.POST)
	public ResponseData createProjectCotegory(ProjectCotegoryData projectCotegoryData){
		ProjectCotegoryEntity projectCotegoryEntity=new ProjectCotegoryEntity(); 
		projectCotegoryEntity=projectCotegoryData.toEntity();//传输类转为实体类
		String id=projectCotegoryService.createProjectCotegory(projectCotegoryEntity);
		projectCotegoryData.setId(id);//放入id
		return new ResponseData(true,projectCotegoryData);
	}
	
	/**
	 * 根据项目类型编号删除项目类型
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/deleteProjectCotegory.service",method=RequestMethod.POST)
	public ResponseData deleteProjectCotegory(String id){
		if(StringUtil.isBlank(id))
		{
			return new ResponseData(false,ExceptionConstant.ID_NULL_CODE,ExceptionConstant.ID_NULL_MESSAGE,null);
		}
		projectCotegoryService.deleteProjectCotegory(id);
		return new ResponseData(true,CommonConstant.DELETE_SUCCESS);
	}
	
	/**
	 * 根据项目类型编号修改项目种类
	 * @param projectCotegoryEntity
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/updateProjectCotegory.service",method=RequestMethod.POST)
	public ResponseData updateProjectCotegory(ProjectCotegoryData projectCotegoryData){
		if(projectCotegoryData==null)
		{
			return new ResponseData(false,ExceptionConstant.DATA_NULL_CODE,ExceptionConstant.DATA_NULL_MESSAGE,null);
		}
		ProjectCotegoryEntity projectCotegoryEntity=new ProjectCotegoryEntity(); 
		projectCotegoryEntity=projectCotegoryData.toEntity();//传输类转为实体类
		projectCotegoryService.updateProjectCotegory(projectCotegoryEntity);
		return new ResponseData(true,projectCotegoryData);
	}
	
	/**
	 * 更具项目类型实体查询项目类型
	 * @return 项目类型传输类集合
	 */
	@ResponseBody
	@RequestMapping(value="/retieveProjectCotegorys.service",method=RequestMethod.POST)
	public ResponseData retieveProjectCotegorys(){
		List<ProjectCotegoryEntity> ProjectCotegoryEntityList=new ArrayList<ProjectCotegoryEntity>();
		ProjectCotegoryEntityList=projectCotegoryService.retieveProjectCotegorys();
		List<ProjectCotegoryData> projectCotegoryDatas=toProjectCotegoryData(ProjectCotegoryEntityList);
		return new ResponseData(true,projectCotegoryDatas);
	}
	
	/**
	 * 根据项目类型编号查询项目类型
	 * @param id
	 * @return 项目类型传输类
	 */
	@ResponseBody
	@RequestMapping(value="/retieveProjectCotegory.service",method=RequestMethod.POST)
	public ResponseData retieveProjectCotegory(String id){
		ProjectCotegoryEntity projectCotegoryEntity=new ProjectCotegoryEntity(); 
		projectCotegoryEntity=projectCotegoryService.retieveProjectCotegory(id);
		ProjectCotegoryData projectCotegoryData=new ProjectCotegoryData();
		projectCotegoryData=toProjectCotegoryData(projectCotegoryEntity);
		return new ResponseData(true,projectCotegoryData);
	}

	/**
	 * 实体类转化为传输类
	 * @return 
	 */
	private ProjectCotegoryData toProjectCotegoryData(ProjectCotegoryEntity projectCotegoryEntity) {
		ProjectCotegoryData projectCotegoryData = new ProjectCotegoryData();
		projectCotegoryData.toProjectCotegoryData(projectCotegoryEntity);
		return projectCotegoryData;
	}
	
	/**
	 * 实体类集合转化为传输类集合
	 * @param DefaultTargetEntitys
	 * @return
	 */
	private List<ProjectCotegoryData> toProjectCotegoryData(List<ProjectCotegoryEntity> projectCotegoryEntity) {
		int size = projectCotegoryEntity.size();
		List<ProjectCotegoryData> projectCotegoryDatas = new ArrayList<ProjectCotegoryData>();
		for (int i = 0; i < size; i++) { // 循环将实体数据转换为传输数据
			ProjectCotegoryData projectCotegoryData = new ProjectCotegoryData();
			projectCotegoryData.toProjectCotegoryData(projectCotegoryEntity.get(i));
			projectCotegoryDatas.add(projectCotegoryData);
		}
		return projectCotegoryDatas;
	}
	
}